<?php

use Illuminate\Database\Eloquent\ModelNotFoundException as ModelNotFoundException;

class AdminRefundController extends AdminController {

	public function getIndex()
	{
		$model = new Refund;
		$result = $model->getAll('customer');

		$data = array('result' => $result);

		$this->layout->title = trans('refund.refund');
		$this->layout->content = View::make('admin.refund.index', $data);
	}

	public function getCreate($id)
	{
		$model  = new Refund;
		$detail = $model->getDetail($id);
		
		$customer = Customer::find($id);
		$data['customer'] = $customer;

		$this->layout->title = trans('refund.add_refund');
		$this->layout->content = View::make('admin.refund.form', $data);
	}

	public function postCreate(){
		$result = array('error' => true, 'message' => 'Only accept ajax request!');

		if(Request::ajax()){
			$data['customer'] = Input::get('customer', false);
			$date             = Input::get('date', false);
			$data['date']     = DateFormat::format($date);
			$data['total']    = Input::get('total', false);
			$id               = Input::get('id', false);

			if($id != false){
				$data['id'] = $id;
			}

			if($data['customer'] != false || $data['date'] != false || $data['total'] != false){
				$model = new Refund;
				$model->saveData($data);

				$result = array('error' => false, 'message' => 'saved');
			}else{
				$result = array('error' => true, 'message' => 'Something error!');
			}
		}

		return json_encode($result);
	}

	public function getDetail($id){
		$model = new Refund;
		$data['result'] = $model->getDetail($id);

		$this->layout->title = trans('refund.detail');
		$this->layout->content = View::make('admin.refund.detail', $data);
	}

	public function postDelete()
	{
		$result = array('error' => true, 'message' => 'Only accept ajax request!');

		if(Request::ajax()){
			$id    = Input::get('id', 0);
			
			$result = array('error' => true, 'message' => 'cannot delete');

			$model = new Refund;
			$refund = $model->findOrFail($id);

			if($refund != false){
				$refund->delete();

				$result = array('error' => false, 'message' => 'deleted');
			}
		}

		return json_encode($result);
	}
}
